package de.edb.ctrl;

import java.io.IOException;
import java.util.Date;

import org.apache.catalina.util.Base64;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import de.edb.dao.DaoException;
import de.edb.dao.LocationDao;
import de.edb.model.Location;

@Controller
public class LocationController {
	private static Logger log = Logger.getLogger(LocationController.class);
	private LocationDao locationDao = null;

    public void setLocationDao(LocationDao locationDao) {
		this.locationDao = locationDao;
	}

	/**
     * <p> Location form request.</p>
     * 
     * <p>Expected HTTP GET and request '/editlocation.html'.</p>
     * @return 
     */
    @RequestMapping(value="/admin/editlocation.html", method=RequestMethod.GET)
    public ModelAndView edit(@RequestParam(required=false) Integer id) {
    	log.debug("editlocation.html");
    	ModelAndView mv = new ModelAndView();
    	if (id == null) {
    		mv.addObject(new Location());
    	} else { 
    		mv.addObject(locationDao.findById(id));
    	}
    	mv.addObject("tab", "locations");
    	mv.setViewName("edit-location");
    	return mv;
    }

    /**
     * <p>Saves a {@link Location}.</p>
     * 
     * <p>Expected HTTP POST and request '/savelocation.html'.</p>
     * @return 
     */
    @RequestMapping(value="/admin/savelocation.html", method=RequestMethod.POST)
    public String save(Location location, Model model, @RequestParam(value="pic", required=true) MultipartFile pic) {
        
    	try{
    		String newPicture = Base64.encode(pic.getBytes());
    		if(newPicture.length()>10) location.setPicture(newPicture);
    	} catch(IOException ex) {
    		System.out.print(ex.getStackTrace());
    	}
    	
    	if (location.getCreated() == null) {
            location.setCreated(new Date());
        }
        locationDao.save(location);
        return "redirect:/location.html?id="+location.getId();
    }

    /**
     * <p>Deletes a Location.</p>
     * 
     * <p>Expected HTTP POST and request '/deletelocation.html'.</p>
     */
    @RequestMapping(value="/admin/deletelocation.html", method=RequestMethod.GET)
    public ModelAndView delete(Location location) {
        try {
			locationDao.delete(location);
	        return new ModelAndView("redirect:/locations.html");
		} catch (DaoException e) {
			ModelAndView mv = new ModelAndView("error");
			mv.addObject("message", e.getMessage());
			mv.addObject("tab", "locations");
			return mv;
		}
    }

    /**
     * <p>Searches for all locations and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/locations.html'.</p>
     */
    @RequestMapping(value="/locations.html", method=RequestMethod.GET)
    public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Locations");
		mv.addObject("message", "Alle Locations");
		mv.addObject("locations", locationDao.findAll());
		mv.addObject("tab", "locations");
		mv.setViewName("list-locations");
		return mv;
    }
    
    /**
     * <p>Searches for a specific location
     * 
     * <p>Expected HTTP GET and request '/locations.html'.</p>
     */
    @RequestMapping(value="/location.html", method=RequestMethod.GET)
    public ModelAndView show(@RequestParam(required=true) Integer id) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("location", locationDao.findById(id));
		mv.addObject("tab", "locations");
		mv.setViewName("show-location");
		return mv;
    }
    

}
